package com.yixin.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.NextVisit;
import com.yixin.hospital.entity.dto.req.nextvisit.AddNextVisitReqDTO;
import com.yixin.hospital.service.NextVisitService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RequestMapping("/next-visit")
@RestController
@Api(value = "预案", tags = "预案")
public class NextVisitController {


    @Resource
    private NextVisitService nextVisitService;

    @PostMapping("/add")
    @ApiOperation(value = "新增预案")
    @Transactional
    public ApiResult<?> addNextVisit(@Valid @RequestBody AddNextVisitReqDTO addNextVisitReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return nextVisitService.add(addNextVisitReqDTO, userName);
    }

    @GetMapping("/get-by-guidance-code")
    @ApiOperation(value = "获取预案")
    public ApiResult<?> getByGuidanceCode(@RequestParam String guidanceCode) {
        return ApiResult.success(nextVisitService.getOne(new LambdaQueryWrapper<NextVisit>().eq(NextVisit::getGuidanceCode, guidanceCode)));
    }

    @GetMapping("/get-user-last-next-visit")
    @ApiOperation(value = "获取用户最后一条预案")
    public ApiResult<?> getUserLastNextVisit(@RequestParam Long userId) {
        return nextVisitService.getUserLastNextVisit(userId);
    }
}
